#include "stm32f10x_conf.h"
//#include "usb_init.h"
__IO uint8_t PrevXferComplete = 1;
__IO uint8_t EP2 = 1;
int main(void)
{
	
	LED0_Config();
	
	Delay_init(72);
	
	USART1_Config();
	Trace(USART1,"USART1_Configuration successed!!\r\n");
	Set_System();
	Trace(USART1, "Set_System successed!!\r\n");
	USB_Interrupts_Config();
	Trace(USART1, "USB_Interrupts_Config successed!!\r\n");
	Set_USBClock();
	Trace(USART1, "Set_USBClock Ssuccessed!!\r\n");
	USB_Init();
	Trace(USART1, "USB_Inited!!\r\n");
	__IO uint8_t Send_Buffer[9];
	int8_t push_times = 0;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	KeyBoard_Config();
	Trace(USART1, "KeyBoard_Configuration successed!!\r\n");
	Timer1_Config();
	Trace(USART1, "Timer1_Config successed!!\r\n");
  while (1)
  {
	  //if ((EP2) && (bDeviceState == CONFIGURED))
	  //{
		//  //EP2 = 0;
		//  //PrevXferComplete = 1;
		//  Send_Buffer[0] = 0x05;
		//  Send_Buffer[1] = rand();
		//  Send_Buffer[2] = 0x01;
		//  Send_Buffer[3] = 0x01;
		//  USB_SIL_Write(EP2_IN, (uint8_t*)Send_Buffer, 8);
		//  SetEPTxValid(ENDP2);
		//  Trace(USART1, "EP2_IN !!\r\n");
		//  LED0(ON);
		//  Delay_ms(500);
		//  LED0(OFF);
		//  Delay_ms(500);
	  //}
	  //LED0(ON);
	  //uint16_t COLUMNKEY = KEYBOARD_GPIO(COLUMN_LINE);
	  //LED0(OFF);
	  //Delay_ms(100);
	  //LED0(ON);
	  //uint16_t ROWKEY = KEYBOARD_GPIO(ROW_LINE);
	  //LED0(OFF);
	  //Trace(USART1, "COLUMN: %x%x,ROW : %x%x \r\n", (uint8_t)(COLUMNKEY >> 8), (uint8_t)COLUMNKEY, (uint8_t)(ROWKEY >> 8), (uint8_t)ROWKEY);
	
	  
	  
	  
	  //uint32_t KEYDATA= KeyBoard_Scan();
	  //Trace(USART1, "COLUMN: %x%x,ROW : %x%x \r\n", (uint8_t)(KEYDATA >> 24), (uint8_t)(KEYDATA>>16), (uint8_t)(KEYDATA >> 8), (uint8_t)KEYDATA);
	  while (PrevXferComplete != 1);
	  if ((PrevXferComplete) && (bDeviceState == CONFIGURED))
	  {
		  PrevXferComplete = 0;
		  Send_Buffer[0] = 0x03;
		  Send_Buffer[1] = rand();
		  Send_Buffer[2] = rand();
		  Send_Buffer[3] = rand();
		  USB_SIL_Write(EP1_IN, (uint8_t*)Send_Buffer, 4);
		  SetEPTxValid(ENDP1);
		  Delay_ms(20);
	  }
	  /*if ((PrevXferComplete) && (bDeviceState == CONFIGURED))
	  {
		  PrevXferComplete = 0;
		  Send_Buffer[0] = 0x05;
		  Send_Buffer[1] = rand();
		  Send_Buffer[2] = rand();
		  Send_Buffer[3] = rand();
		  USB_SIL_Write(EP1_IN, (uint8_t*)Send_Buffer, 4);
		  SetEPTxValid(ENDP1);
		  PrevXferComplete = 1;
		  Delay_ms(100);
	  }
	  if ((PrevXferComplete) && (bDeviceState == CONFIGURED))
	  {
		  PrevXferComplete = 0;
		  Send_Buffer[0] = 0x07;
		  Send_Buffer[1] = rand();
		  Send_Buffer[2] = rand();
		  Send_Buffer[3] = rand();
		  USB_SIL_Write(EP1_IN, (uint8_t*)Send_Buffer, 9);
		  SetEPTxValid(ENDP1);
		  PrevXferComplete = 1;
		  Delay_ms(100);
	  }
	  
	  if ((PrevXferComplete) && (bDeviceState == CONFIGURED))
	  {
		  
		  Send_Buffer[0] = 0x01;
		  Send_Buffer[1] = 0x00;//modifier
		  Send_Buffer[2] = 0x00;//reserved
		  Send_Buffer[3] = 0x00;//keycode1
		  Send_Buffer[4] = 0x00;//keycode2
		  Send_Buffer[5] = 0x00;//keycode3
		  Send_Buffer[6] = 0x00;//keycode4
		  Send_Buffer[7] = 0x00;//keycode5
		  Send_Buffer[8] = 0x00;//keycode6
	
		  
		  USB_SIL_Write(EP1_IN, (uint8_t*)Send_Buffer, 9);
		  SetEPTxValid(ENDP1);
		  Delay_ms(20);
	  }
	  if ((PrevXferComplete) && (bDeviceState == CONFIGURED))
	  {
		  Send_Buffer[0] = 0x01;
		  Send_Buffer[1] = 0x00;//modifier
		  Send_Buffer[2] = 0x00;//reserved
		  Send_Buffer[3] = 0x00;//keycode1
		  Send_Buffer[4] = 0x00;//keycode2
		  Send_Buffer[5] = 0x00;//keycode3
		  Send_Buffer[6] = 0x00;//keycode4
		  Send_Buffer[7] = 0x00;//keycode5
		  Send_Buffer[8] = 0x00;//keycode6
		  USB_SIL_Write(EP1_IN, (uint8_t*)Send_Buffer, 9);
		  SetEPTxValid(ENDP1);
		  Delay_ms(20);
	  }*/
  }
}
